package com.definity.toolkit.domain;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class DynamicDomain<T extends Serializable> extends Domain<T> {

	private final Map<String, Object> properties = new HashMap<String, Object>();
	
	public DynamicDomain() {

	}
	
	@Override
	@SuppressWarnings("unchecked")
	public final T getId() {
		return (T) properties.get("id");
	}
	
	public final void setId(T id) {
		properties.put("id", id);
	}
	
	public final Object getProperty(String name) {
		return properties.get(name);
	}

	@SuppressWarnings("unchecked")
	protected final <R> R getProperty(Class<R> type, String name) {
		return (R) properties.get(name);
	}
	
	public final void setProperty(String name, Object value) {
		properties.put(name, value);
	}
	
	public final Map<String, Object> getProperties() {
		return Collections.unmodifiableMap(properties);
	}

	public final DynamicDomain<Serializable> getMetadata() {
		DynamicDomain<Serializable> metadata = new DynamicDomain<Serializable>();
		
		for (Entry<String, Object> e : properties.entrySet()) {
			if (e.getValue() == null)
				continue;
			
			metadata.setProperty(e.getKey(), e.getValue().getClass());
		}
		
		return metadata;
	}
	
	public final String toString() {
		return properties.toString();
	}
	
}
